﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Tracy.CoreLib
{
    public class TracyException : Exception
    {
        public const int EXCEPTION_XML_NODE_NOT_FOUND = 101;
        public const int EXCEPTION_ID_ALREADY_SET = 102;
        public const int EXCEPTION_FAILED_TO_REFRESH_RSS = 103;
        public const int EXCEPTION_THREAD_STILL_RUNNING = 104;
        public const int EXCEPTION_XML_VALUE_FORMAT = 105;
        public const int EXCEPTION_SYSTEM_PARAMETER_NOT_FOUND = 106;
        private string cause;

        public string Cause
        {
            get { return cause; }
            //set { cause = value; }
        }


        private int errorcode;

        public int ErrorCode
        {
            get { return errorcode; }
            //set { errorcode = value; }
        }
        private string description;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        
        public TracyException(int errorcode, string cause)
            : base(GenerateDescription(errorcode,cause))
        {
            this.cause = cause;
            this.errorcode = errorcode;
            
        }

        public static string GenerateDescription(int errorcode, string cause)
        {
            if (cause == null || cause == "") return GenerateDescription(errorcode);
            switch (errorcode)
            {
                case EXCEPTION_XML_NODE_NOT_FOUND: return "错误 101: Xml节点 '" + cause + "' 不存在";
                default: return GenerateDescription(errorcode) + "\n" + cause;
            }
        }

        public static string GenerateDescription(int errorcode)
        {
            switch (errorcode)
            {
                case EXCEPTION_XML_NODE_NOT_FOUND: return "错误 101: Xml节点不存在";
                case EXCEPTION_ID_ALREADY_SET: return "错误 102: ID已经被设置";
                case EXCEPTION_FAILED_TO_REFRESH_RSS: return "错误 103: 无法刷新RSS源";
                case EXCEPTION_THREAD_STILL_RUNNING: return "错误 104: 线程仍在工作";
                case EXCEPTION_XML_VALUE_FORMAT: return "错误 105: 数据格式不正确";
                case EXCEPTION_SYSTEM_PARAMETER_NOT_FOUND: return "错误106: 未找到系统参数";
                default: return "错误 000: 未知的异常";
            }
        }

        public TracyException(int errorcode, string cause, string description)
            : base(description)
        {
            this.cause = cause;
            this.errorcode = errorcode;
        }

        public TracyException(string description)
            : base(description)
        {
        }

        public TracyException(int errorcode)
            : base(GenerateDescription(errorcode))
        {
            this.errorcode = errorcode;
        }
    }
}
